package com.jsdzt;


import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
 * @author ：LiuShihao
 * @date ：Created in 2021/12/16 9:24 上午
 * @desc ：文本框
 */
public class Editor extends JInternalFrame implements DocumentListener,Runnable{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    Runtime r=Runtime.getRuntime();
    Ys1901lixintong111 parent;

    Document doc;//The Document is a container for text that servesas the model for swing text components.
    StyledDocument styledDoc = new DefaultStyledDocument();//Interface for a generic styled document.
    Thread th;

    private int hour=0;//时
    private int minute=0;//分
    private int second=0;//秒
    //标签
    private JLabel lblTime;//用时
    private JLabel lblError;//错误字数
    private JLabel lblWord;	//总字数
    private JLabel lblM;
    private JLabel lblRight;//正确字数
    private JLabel lblCount;//

    //面板
    private JPanel pShowData;//显示文本面板
    private JPanel pAttrib;//参数显示面板

    JTextPane txtOriginal;//原始文本
    JTextPane txtNewText;//输入文本

    //信息条区域
    private JTextField txtMW;//每分钟多少字
    private JTextField txtError;//错误字数显示条
    private JTextField txtRight;//正确字数显示条
    private JTextField txtTime;//时间计数显示条
    private JTextField txtCount;//总输入字数

    //记录输入的正确和错误的个数据
    private int rightWord=0;
    private int errorWord=0;
    private int rightCount=0;
    private int errorCount=0;
    private int wordCount=0;
    //临时
    @SuppressWarnings("unused")
    private boolean temp=true;


    public Editor(final Ys1901lixintong111 parent ){
        this.parent=parent;
        th=new Thread(this);


        //组建Editor用户界面....
        setBounds(0,0,0,0);
        this.setPreferredSize(new java.awt.Dimension(657, 325));//内部窗体大小657,325
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
        getContentPane().setLayout(null);
        this.setVisible(true);
        {
            pAttrib = new JPanel();
            getContentPane().add(pAttrib, new GridBagConstraints(1, 0, 12, 3, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
            FlowLayout pAttribLayout = new FlowLayout();
            pAttrib.setBorder(BorderFactory.createTitledBorder(""));
            pAttrib.setLayout(pAttribLayout);
            pAttrib.setBounds(-2, 33, 658, 42);
            {
                lblTime = new JLabel();
                pAttrib.add(lblTime);
                lblTime.setText("用时：");
                lblTime.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtTime = new JTextField(8);
                pAttrib.add(txtTime);
                txtTime.setText("00:00:00");
                txtTime.setEditable(false);
                txtTime.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblCount = new JLabel();
                pAttrib.add(lblCount);
                lblCount.setText("总字数:");
                lblCount.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtCount = new JTextField();
                pAttrib.add(txtCount);
                txtCount.setColumns(6);
                txtCount.setText("0");
                txtCount.setEditable(false);
                txtCount.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblRight = new JLabel();
                pAttrib.add(lblRight);
                lblRight.setText("正确:");
                lblRight.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtRight = new JTextField();
                pAttrib.add(txtRight);
                txtRight.setColumns(4);
                txtRight.setText("0");
                txtRight.setEditable(false);
                txtRight.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblError = new JLabel();
                pAttrib.add(lblError);
                lblError.setText("错误:");
                lblError.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtError = new JTextField();
                pAttrib.add(txtError);
                txtError.setColumns(4);
                txtError.setText("0");
                txtError.setEditable(false);
                txtError.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblM = new JLabel();
                pAttrib.add(lblM);
                lblM.setText("每分钟");
                lblM.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtMW = new JTextField();
                pAttrib.add(txtMW);
                txtMW.setColumns(4);
                txtMW.setText("0");
                txtMW.setEditable(false);
                txtMW.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblWord = new JLabel();
                pAttrib.add(lblWord);
                lblWord.setText("个字");
                lblWord.setFont(new java.awt.Font("新宋体", 0, 12));
            }

        }
        {
            pShowData = new JPanel();//建一个面板来装两个jtextpane
            getContentPane().add(pShowData,new GridBagConstraints(1,1,12,10,0.0,0.0,
                    GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0, 0, 0, 0),0,0));
            GridLayout pShowDataLayout = new GridLayout(2, 1);
            pShowDataLayout.setHgap(5);
            pShowDataLayout.setVgap(5);
            pShowDataLayout.setColumns(1);
            pShowDataLayout.setRows(2);
            pShowData.setLayout(pShowDataLayout);
            pShowData.setBounds(5, 82, 644, 231);
            {
                txtOriginal = new JTextPane(styledDoc);//导入文本
                pShowData.add(txtOriginal);//把pane放到Jpanel上
                txtOriginal.setPreferredSize(new java.awt.Dimension(600, 0));
                txtOriginal.setEditable(false);//设置为不可修改
            }
            {
                txtNewText = new JTextPane();
                pShowData.add(txtNewText);
                txtNewText.setPreferredSize(new java.awt.Dimension(600, 0));
                Document doc=txtNewText.getDocument();
                doc.addDocumentListener(this);//给文本添加监听器
                //给输入文本框添加键盘监听器
                txtNewText.addKeyListener(new KeyAdapter(){
                    public void keyPressed(KeyEvent e)
                    {
                        PublicData.curkey=String.valueOf(e.getKeyChar());//转换成string

                    }
                    public void keyTyped(KeyEvent e) {
//						PublicData.curkey=String.valueOf(e.getKeyChar());
                    }
                    public void keyReleased(KeyEvent e)
                    {
                        PublicData.curkey="";//转换成string
                        parent.repaint();

                    }
                });

            }
        }
        {

            txtOriginal.setText(PublicData.Text);
            PublicData.key=txtOriginal.getText().substring(0,1);

            //启动线程，文本输入满时清空
            Empty empty=new Empty();
            empty.start();


        }
        this.pack();
        //启动线程
        th.start();
    }

    //判断正确和错误的字.并改变它们的颜色
    public void chDocs(int xLen,int yLen,String xStr,String yStr)
    {
        SimpleAttributeSet attrSet=new SimpleAttributeSet();
        rightWord=0;
        errorWord=0;
        for(int i=1;i<=xLen;i++)
        {
            if(i<=yLen){//数据验证
                if(xStr.substring(i-1,i).equals(yStr.substring(i-1,i))){
                    rightWord++;
                    StyleConstants.setForeground(attrSet,Color.BLUE);
                }
                else{
                    errorWord++;
                    StyleConstants.setForeground(attrSet,Color.RED);
                }
                StyleConstants.setUnderline(attrSet,true);
            }
            else{
                StyleConstants.setForeground(attrSet,Color.BLACK);
                StyleConstants.setUnderline(attrSet,false);
            }
            styledDoc.setCharacterAttributes(i-1,1,attrSet,true);
        }
        //设置正确、错误字数
        txtRight.setText((rightWord+rightCount)+"");
        txtError.setText((errorWord+errorCount)+"");
        //计算和设置每分钟正确打字数
        int c=0;
        for(int i=0;i<minute;i++){
            c=c+60;
        }
        c=c+second;
        double s=(double)c/60;
        txtMW.setText(""+Math.round(((double)Integer.parseInt(txtRight.getText()))/s));
    }

    //实现DocumentListener所有的方法..用于监听输入文本...，获取即将输入的下一个字符是啥,三个监听都是用来动态改变键盘的下一个输入key是什么
    public void changedUpdate(DocumentEvent e){
        PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                txtNewText.getText().length()+1);
        this.parent.repaint();
    }
    public void insertUpdate(DocumentEvent e){
        PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                txtNewText.getText().length()+1);
        this.parent.repaint();
    }

    public void removeUpdate(DocumentEvent e){
        PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                txtNewText.getText().length()+1);
        this.parent.repaint();
    }

    //数据验证
    private void dataValidate(){
        chDocs(txtOriginal.getText().length(),txtNewText.getText()
                .length(),txtOriginal.getText(),txtNewText.getText());
        if(txtOriginal.getText().length()>txtNewText.getText().length()){
            PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                    txtNewText.getText().length()+1);
            txtCount.setText((txtNewText.getText().length()+wordCount)+"");
        }
    }


    //计时器
    public  void run(){
        try{
            while(true){

                Thread.sleep(1000);
                second++;
                if(second==60){
                    second=0;
                    minute++;
                    if(minute==60){
                        minute=0;
                        hour++;
                        if(hour==1000){
                            second=0;
                            minute=0;
                            hour=0;
                        }
                    }
                }
                txtTime.setText(((hour<=9)?"0":"")+hour+":"+((minute<=9)?"0":"")
                        +minute+":"+((second<=9)?"0":"")+second);
            }
        }
        catch (InterruptedException e){}
    }

    //清空文本框的线程
    class Empty extends Thread{
        public void run(){
            while(true){
                try{
                    Thread.sleep(100);
                    dataValidate();
                    //当文本打完时清空输入框，更新初始文本
                    if(txtNewText.getText().length()>=txtOriginal.getText().length()){
                        txtNewText.setText("");
                        PublicData.Text=PublicData.getText((int)(Math.random()*5+1));
                        txtOriginal.setText(PublicData.Text);
                        parent.repaint();
                    }
                }
                catch(InterruptedException e){}
            }
        }
    }
}
